<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Services\Tools\CommonService;
use App\Services\Tools\ExportService;
use Illuminate\Http\Request;    //引用http请求类
use App\Exceptions\BasicException;  //引入框架异常处理类
use Illuminate\Support\Facades\Hash;    //引入hash
use App\Services\Cache\AdminCacheService;   //admin缓存
use App\Services\Tools\JwtService;    //引入JWT服务
use App\Services\Tools\FileService;   //引入文件处理类
use App\Response\Response;

class NewFishController extends BaseController {
    public $type, $pot_name, $pool_array;

    public function __construct(Request $request) {
        $this->type = $request->input('type', 3);
        switch ($this->type) {
            case 3:
                $this->pot_name = '2元奖池';
                break;
            case 4:
                $this->pot_name = '2元独立奖池';
                break;
            default:
                $this->pot_name = '2元奖池';
        }

        $this->pool_array = [
            3 => '2元奖池',
            4 => '2元独立奖池',
        ];
    }

    /**
     * @api {post} /new_fish/new_this_list  新钓鱼本期奖池列表
     * @apiVersion 0.1.0
     * @apiName 新钓鱼本期奖池列表
     * @apiGroup 游戏管理
     * @apiParam {number} type    奖池类型：3 ：2元奖池  4 ：2元独立奖池
     * @apiSuccessExample 成功响应
      {
          "code": 200,
          "data": {
          "total（理论投入总金币）": 2000000,
          "total_count(理论总次数)": 10000,
          "output(理论产出总金币)": 100000,
          "ratio（理论产出比）": 0.05,
          "total_real（实际投入总金币）": 55638000,
          "total_count_real（实际总次数）": 278190,
          "output_real（实际产出总金币）": 2781900,
          "ratio_real（实际产出比）": 0.05,
          "name（奖池名称）": "2元奖池",
          "type（奖池类型：3  2元奖池  4 2元独立奖池）": 3,
          "list（信息列表）": [
                  {
                      "id（主键id）": 34,
                      "number（数量）": 999999989532,
                      "gift_id（奖品id）": 156,
                      "ratio（爆率）": 26680,
                     "chance（爆率百分比）":  "26.6811%",
                      "total_gift（中奖总金币数）": "2781900",
                      "total_count（中奖总个数）": "278190",
                      "chance_real（实际概率）":  "26.7549%",
                      "gift_name（奖品名字）": "音符",
                      "price（奖品价格）": 10,
                      "picture（奖品图片）": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/5e61e230d6521.png",
                      "special（奖品动效图）": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/5e61e23428674.png"
                  }
              ]
          }
      }
     *
     */
    public function new_this_list(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'type' => 'filled|numeric|in:3,4',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $all_data = [];
        $list = app('repo_game_jackpot')->newGiftList($this->type);
        if ($list) {
            $where_log[] = ['pool_type', $this->type];
            $gift_ids = array_column($list, 'gift_id');
            $where_log[] = [function ($query) use ($gift_ids) {
                $query->whereIn('gift_id', $gift_ids);
            }];
            $game_log_result = app('repo_game_log')->list($where_log);
            $game_log = [];
            $total_count_real = array_sum(array_column($game_log_result, 'total_count'));
            $total_real = 0;
            if ($game_log_result) {
                foreach ($game_log_result as &$value) {
                    $value['chance_real'] = round($value['total_count'] / $total_count_real * 100, 4);
                    $game_log[$value['gift_id']] = $value;
                    $total_real += $value['total_gift'];
                }
            }
            $total_ratio = array_sum(array_column($list, 'ratio'));
            $total_count = 10000;
            //todo 这个价格要从参数表里面获取,现在先写死
            $price = 200;
            $output = $output_real = 0;
            foreach ($list as $key => &$val) {
                $val['chance'] = $val['ratio'] > 0 ? round($val['ratio'] / $total_ratio * 100, 4) : 0;
                $output += $total_count * $val['chance'] / 100 * $val['gift']['price'];
                $val['chance'] = $val['chance'] . '%';
                $val['total_gift'] = $game_log[$val['gift_id']]['total_gift'] ?? 0;
                $val['total_count'] = $game_log[$val['gift_id']]['total_count'] ?? 0;
                $val['chance_real'] = $game_log[$val['gift_id']]['chance_real'] ?? 0;
                $val['chance_real'] = $val['chance_real'] . '%';
                $output_real += $val['total_gift'];
                $val['gift_name'] = $val['gift']['name'];
                $val['price'] = $val['gift']['price'];
                $val['picture'] = $val['gift']['picture'];
                $val['special'] = $val['gift']['special'];
                unset($val['gift']);
            }
            $total = $total_count * $price;
            $all_data = [
                'total' => $total,
                'total_count' => $total_count,
                'output' => $output,
                'ratio' => round($output / $total, 4),
                'total_real' => $total_count_real * $price,
                'total_count_real' => $total_count_real,
                'output_real' => $output_real,
                'ratio_real' => $total_count_real > 0 ? round($output_real / ($total_count_real * $price), 4) : 0,
            ];
        }
        $all_data['name'] = $this->pot_name;
        $all_data['type'] = $this->type;
        $all_data['list'] = $list;
        return Response::sendData($all_data);
    }


    /**
     * @api {post} /new_fish/add_new_prize    新增新钓鱼奖池
     * @apiVersion 0.1.0
     * @apiName 新增新钓鱼奖池
     * @apiGroup 游戏管理
     * @apiParam {number} type   礼物类型：3  2元奖池  4  2元独立奖池，必传
     * @apiParam {number} gift_id   礼物ID，必传
     * @apiParam {number} number   数量，必传
     * @apiParam {number} ratio   爆率，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93060  新增新钓鱼奖池失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93060,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function addNewPrize(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'type' => 'required|in:3,4',
                'gift_id' => 'required|numeric',
                'number' => 'required|numeric',
                'ratio' => 'required|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $param = $request->only(['gift_id', 'number', 'ratio', 'type']);
        app('repo_game_jackpot')->addNewPrize($param);
        return Response::sendData(true);
    }


    /**
     * @api {post} /new_fish/save_new_prize   编辑新钓鱼奖池
     * @apiVersion 0.1.0
     * @apiName 编辑新钓鱼奖池
     * @apiGroup 游戏管理
     * @apiParam {number} id   主键id，必传
     * @apiParam {number} gift_id   礼物ID，必传
     * @apiParam {number} number   数量，必传
     * @apiParam {number} ratio   爆率，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93061  编辑新钓鱼奖池失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 93061,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function saveNewPrize(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'type' => 'required|in:3,4',
                'gift_id' => 'required|numeric',
                'number' => 'required|numeric',
                'ratio' => 'required|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $param = $request->only(['id', 'gift_id', 'number', 'ratio']);
        app('repo_game_jackpot')->saveNewPrize($param);
        return Response::sendData(true);
    }


    /**
     * @api {post} /new_fish/del_new_prize 删除新钓鱼奖池
     * @apiVersion 0.1.0
     * @apiName 删除新钓鱼奖池
     * @apiGroup 游戏管理
     *
     * @apiParam {string} ids   主键（多个用逗号隔开），必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93063  删除礼物失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93063,
     *          "message": "errorMsg"
     *      }
     */

    public function delNewPrize(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'ids' => 'required',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $ids = $request->input('ids');
        $arr_ids = explode(',', $ids);
        app('repo_game_jackpot')->delNewPrize($arr_ids);
        return Response::sendData(true);
    }

    /**
     * @api {post} /new_fish/edit_new_all_list  新钓鱼一键修改礼物列表
     * @apiVersion 0.1.0
     * @apiName 新钓鱼一键修改礼物列表
     * @apiGroup 游戏管理
     * @apiParam {number} type  类型：3  2元奖池  4  2元独立奖池，必传
     * @apiParam {number} plan_id  方案id，非必传
     * @apiSuccessExample 成功响应
      {
          "code": 200,
          "data": {
              "type（类型：3  2元奖池  4  2元独立奖池）": "3",
              "pot_name（奖池名称）": "2元独立奖池",
              "plan_list(奖池方案列表)": [
                  {
                  "id（主键id）": 3,
                  "plan_name（名字）": "正常",
                  "add_time（添加时间）": 1594803335,
                  "type（类型：3  2元奖池  4  2元独立奖池）": 4
                  }
              ],
              "gift_list（要编辑的奖品列表）": [
                  {
                  "id（主键id）": 50,
                  "number（数量）": 999999998,
                  "gift_id（奖品id）": 156,
                  "ratio（爆率）": 26680,
                  "name（奖品名字）": "音符",
                  "price（奖品价格）": 10,
                  "picture（奖品图片）": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/5e61e230d6521.png"
                  }
              ]
          }
      }
     *
     */
    public function edit_new_all_list(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'type' => 'required|in:3,4',
                'plan_id' => 'filled|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $plan_id = $request->input('plan_id', '');
        if ($plan_id) {
            $giftList = app('repo_game_jackpot_plan_detail')->getPlanDetailList($plan_id);
        } else {
            $giftList = app('repo_game_jackpot')->newGiftList($this->type);
        }
        if ($giftList) {
            foreach ($giftList as $key => &$value) {
                $value['name'] = $value['gift']['name'];
                $value['price'] = $value['gift']['price'];
                $value['picture'] = $value['gift']['picture'];
                unset($value['gift']);
            }
        }
        $getPlanList = app('repo_game_jackpot_plan')->getPlanList($this->type);
        $data = [
            'type' => $this->type,
            'pot_name' => $this->pot_name,
            'plan_list' => $getPlanList,
            'gift_list' => $giftList,
        ];
        return Response::sendData($data);
    }

    /**
     * @api {post} /new_fish/do_edit_new_all  新钓鱼一键修改礼物
     * @apiVersion 0.1.0
     * @apiName 新钓鱼一键修改礼物
     * @apiGroup 游戏管理
     * @apiParam {array} list  编辑的礼物列表，必传
     * @apiParam {number} type  类型：3  2元奖池  4  2元独立奖池，必传
     * @apiParam {string} plan_name  方案名字，非必传
     * @apiSuccessExample 成功响应
     * {
          "code": 200,
          "data": true
     * }
     *
     */
    public function do_edit_new_all(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'type' => 'required|in:3,4',
                'list' => 'required|array',
                'plan_name' => 'filled',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $plan_data = [];
        $list = $request->input('list');
        $plan_name = $request->input('plan_name', '');
        if ($list) {
            foreach ($list as $key => &$val) {
                $edit_where[] = ['gift_id', $val['gift_id']];
                $edit_where[] = ['type', $val['type']];
                $edit_arr['number'] = $val['number'];
                $edit_arr['ratio'] = $val['ratio'];
                app('repo_game_jackpot')->editNewAllGifts($edit_where, $edit_arr);
                $plan_data [] = [
                    'gift_id' => $val['gift_id'],
                    'number' => $val['number'],
                    'type' => $this->type,
                    'ratio' => $val['ratio'],
                ];
            }
        }
        if ($plan_name) {
            $new_plan = [
                'plan_name' => $plan_name,
                'add_time' => time(),
                'type' => $this->type
            ];
            $add_plan = app('repo_game_jackpot_plan')->addNewPlan($new_plan);
            if ($add_plan && $plan_data) {
                foreach ($plan_data as $k => &$v) {
                    $v['plan_id'] = $add_plan->id;
                }
                app('repo_game_jackpot_plan_detail')->addNewPlanDetail($plan_data);
            }
        }
        return Response::sendData(true);
    }

    /**
     * @api {post} /new_fish/giving_gifts  新钓鱼送礼物
     * @apiVersion 0.1.0
     * @apiName 新钓鱼送礼物
     * @apiGroup 游戏管理
     * @apiParam {number} type  类型：3  2元奖池  4  2元独立奖池，必传
     * @apiParam {number} user_code  用户编号，必传
     * @apiParam {number} number  数量，必传
     * @apiParam {number} gift_id  礼物ID，必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": true
     * }
     *
     */
    public function givingGifts(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'type' => 'required|in:3,4',
                'user_code' => 'required|numeric',
                'number' => 'required|numeric|min:1',
                'gift_id' => 'required|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $param = $request->only(['type', 'user_code', 'number', 'gift_id']);
        $where_user[] = ['user_code', $param['user_code']];
        $user_id = app('repo_user_data')->getUserId($where_user);
        $where_gift[] = ['gift_id', $param['gift_id']];
        $where_gift[] = ['type', $param['type']];
        app('repo_game_jackpot')->newOneGift($where_gift);
        $where_gift[] = ['user_id', $user_id];
        $game_jackpot_person = app('repo_game_jackpot_person')->getOneInfo($where_gift);
        if ($game_jackpot_person) {
            app('repo_game_jackpot_person')->addNumber($where_gift, $param['number'], 'number');
        } else {
            $arr = [
                'gift_id' => $param['gift_id'],
                'number' => $param['number'],
                'user_id' => $user_id,
                'add_time' => time(),
                'type' => $param['type']
            ];
            app('repo_game_jackpot_person')->add($arr);
        }
        return Response::sendData(true);
    }

    /**
     * @api {post} /new_fish/indepen_user_prize_list  新钓鱼独立用户奖池列表
     * @apiVersion 0.1.0
     * @apiName 新钓鱼独立用户奖池列表
     * @apiGroup 游戏管理
     * @apiParam {number} type  类型：3  2元奖池  4  2元独立奖池，必传
     * @apiSuccessExample 成功响应
      {
          "code": 200,
          "data": {
              "pot_name(奖池名字)": "2元独立奖池",
              "type（奖池类型）": "4",
              "list（奖池列表）": [
                  {
                  "id（主键id）": 10,
                  "add_time（添加时间）": "2020-07-17 09:50:57",
                  "state（是否参与：1参与 2不参与）": 1,
                  "type（奖池类型）": 4,
                  "pool_type（类型）": 4,
                  "user_code（用户编号）": 793781,
                  "nickname（昵称）": "毛22",
                  "head_picture（用户头像）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/b5311f1699be8ca04664f591ae4a2e1e.png",
                  "pool_name（奖池名称）": "2元独立奖池"
                  }
              ]
         }
      }
     */
    public function indepen_user_prize_list(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'type' => 'filled|in:3,4',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $type = $request->input('type', 4);
        if ($type == 3) {
            $pol_name = '2元奖池';
        } else {
            $pol_name = '2元独立奖池';
        }
        $where[] = ['type', $type];
        $list = app('repo_game_special_user')->getGiftList($where);
        if ($list) {
            foreach ($list as $key => &$val) {
                $val['user_code'] = $val['user_data']['user_code'];
                $val['nickname'] = $val['user_data']['nickname'];
                $val['head_picture'] = $val['user_data']['head_picture'];
                $val['add_time'] = date('Y-m-d H:i:s', $val['add_time']);
                if ($type == 3) {
                    $val['pool_name'] = '2元奖池';
                } else {
                    $val['pool_name'] = '2元独立奖池';
                }
                unset($val['user_data']);
                unset($val['user_id']);
            }
        }
        $data = [
            'pot_name' => $pol_name,
            'type' => $type,
            'list' => $list,
        ];
        return Response::sendData($data);
    }



    /**
     * @api {post} /new_fish/add_indepen_user_pool  新钓鱼新增独立奖池用户
     * @apiVersion 0.1.0
     * @apiName 新钓鱼新增独立奖池用户
     * @apiGroup 游戏管理
     * @apiParam {number} type  类型：3  2元奖池  4  2元独立奖池，必传
     * @apiParam {number} user_code  用户编号，必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": true
     * }
     *
     */
    public  function  add_indepen_user_pool(Request $request){
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'type' => 'required|in:3,4',
                'user_code' => 'required|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $param = $request->only(['type','user_code']);
        $where_user[] =['user_code',$param['user_code']] ;
        $user_id = app('repo_user_data')->getUserId($where_user);
        $where[] =['user_id',$user_id] ;
        $where[] =['type',$param['type']] ;
        app('repo_game_special_user')->delWhere($where);
        $param = [
            'user_id' => $user_id,
            'pool_type' => $param['type'],
            'type' => $param['type'],
            'add_time'=>time()
        ];
        app('repo_game_special_user')->addUserPool($param);
        return Response::sendData(true);
    }


    /**
     * @api {post} /new_fish/del_indepen_user_pool  新钓鱼删除独立奖池用户
     * @apiVersion 0.1.0
     * @apiName 新钓鱼删除独立奖池用户
     * @apiGroup 游戏管理
     * @apiParam {number} id  用户主键，必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": true
     * }
     *
     */

    public function del_indepen_user_pool(Request $request){
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $id = $request->input('id');
        $where[] =['id',$id] ;
        app('repo_game_special_user')->delWhere($where);
        return Response::sendData(true);
    }

    /**
     * @api {post} /new_fish/new_win_prize_list  新钓鱼中奖记录列表
     * @apiVersion 0.1.0
     * @apiName 新钓鱼中奖记录列表
     * @apiGroup 游戏管理
     * @apiParam {string} keyword  用户编号或者昵称，非必传
     * @apiParam {number} gift_id  礼物id，非必传
     * @apiParam {string} start_time  开始时间，非必传
     * @apiParam {string} end_time  开始时间，非必传
     * @apiParam {number} page  页数，非必传
     * @apiParam {number} perPage  每页数量，非必传
     * @apiSuccessExample 成功响应
      {
          "code": 200,
          "data": {
              "list": [
                  {
                  "id(主键id)": 2149,
                  "gift_id（礼物id）": 156,
                  "number（礼物数量）": 29,
                  "total（礼物价值）": 290,
                  "cost（花费金币）": 5800,
                  "add_time（时间）": "2020-08-26 14:41:07",
                  "gold_price_id": 1588373,
                  "pool_type（奖池类型）": 3,
                  "type（类型）": 3,
                  "user_code（用户编号）": 188652,
                  "nickname（用户昵称）": "天天启哦哦",
                  "gift_name（礼物名字）": "音符",
                  "price（礼物价格）": 10
                  }
              ],
              "meta": {
                  "page（页数）": 1,
                  "limit（每页的数量）": 20,
                  "next（下一页）": null,
                  "total（数据总数）": 1
              }
          }
      }
     */
    public function new_win_prize_list(Request $request) {

        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'keyword' => 'filled',
                'gift_id' => 'filled|numeric',
                'start_time' => 'filled',
                'end_time' => 'filled',
                'page' => 'filled|numeric',
                'perPage' => 'filled|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $keyword = $request->input('keyword', '');
        $gift_id = $request->input('gift_id', '');
        $start_time = $request->input('start_time', '');
        $end_time = $request->input('end_time', '');
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        if ($gift_id) {
            $where_game[] = ['gift_id', $gift_id];
        }
        $time = CommonService::transTime($start_time, $end_time);
        $search_time = [$time['start_at'], $time['end_at']];
        $where_game[] = [function ($query) use ($search_time) {
            $query->whereBetween('add_time', $search_time);
        }];

        $list = app('repo_game_log')->getGameLogList($where_game, $keyword, $page, $perPage);
        if ($list) {
            foreach ($list as $key => &$value) {
                $value->user_code = $value->user_data->user_code;
                $value->nickname = $value->user_data->nickname;
                $value->gift_name = $value->gift->name;
                $value->price = $value->gift->price;
                $value->add_time = date('Y-m-d H:i:s');
                unset($value->gift);
                unset($value->user_data);
                unset($value->user_id);
            }
        }
        return Response::sendList($list);
    }
    /**
     * @api {get} /new_fish/new_win_prize_export  新钓鱼导出中奖记录
     * @apiVersion 0.1.0
     * @apiName 新钓鱼导出中奖记录
     * @apiGroup 游戏管理
     * @apiParam {string} keyword  用户编号或者昵称，非必传
     * @apiParam {number} gift_id  礼物id，非必传
     * @apiParam {string} start_time  开始时间，非必传
     * @apiParam {string} end_time  开始时间，非必传
     * @apiParam {number} page  页数，非必传
     * @apiParam {number} perPage  每页数量，非必传
     * @apiSuccessExample 成功响应
     * 导出一个xls格式的新钓鱼中奖记录数据表格
     */
    public function new_win_prize_export(Request $request) {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'keyword' => 'filled',
                'gift_id' => 'filled|numeric',
                'start_time' => 'filled',
                'end_time' => 'filled',
                'page' => 'filled|numeric',
                'perPage' => 'filled|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $keyword = $request->input('keyword', '');
        $gift_id = $request->input('gift_id', '');
        $start_time = $request->input('start_time', '');
        $end_time = $request->input('end_time', '');
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        if ($gift_id) {
            $where_game[] = ['gift_id', $gift_id];
        }
        $time = CommonService::transTime($start_time, $end_time);
        $search_time = [$time['start_at'], $time['end_at']];
        $where_game[] = [function ($query) use ($search_time) {
            $query->whereBetween('add_time', $search_time);
        }];

        $list = app('repo_game_log')->getGameLogList($where_game, $keyword, $page, $perPage);
        if ($list) {
            foreach ($list as $key => &$value) {
                $value->user_code = $value->user_data->user_code;
                $value->nickname = $value->user_data->nickname;
                $value->gift_name = $value->gift->name;
                $value->price = $value->gift->price.' 金币';
                $value->add_times = date('Y-m-d H:i:s',$value->add_time);
                unset($value->gift);
                unset($value->user_data);
                unset($value->user_id);
                unset($value->gift_id);
                unset($value->total);
                unset($value->cost);
                unset($value->gold_price_id);
                unset($value->pool_type);
                unset($value->type);
                unset($value->add_time);
            }
            $list= $list->toArray();
        }
        if (isset($list['data'])){
            $list= $list['data'];
            $headings = [
                '序号',
                '奖品数量',
                '用户编号',
                '用户昵称',
                '礼物名称',
                '礼物价格',
                '中奖时间',
            ];
            return app('repo_export')->export(7, $request->all(), $headings, $list);
        }
    }

    /**
     * @api {post} /new_fish/user_daily_summary  新钓鱼用户产出日汇总
     * @apiVersion 0.1.0
     * @apiName 新钓鱼用户产出日汇总
     * @apiGroup 游戏管理
     * @apiParam {number} user_code  用户编号，非必传
     * @apiParam {string} nickname  用户昵称，非必传
     * @apiParam {number} pool_type  奖池类型，非必传
     * @apiParam {string} start_time  开始时间，非必传
     * @apiParam {string} end_time  开始时间，非必传
     * @apiParam {number} page  页数，非必传
     * @apiParam {number} perPage  每页数量，非必传
     * @apiSuccessExample 成功响应
      {
          "code": 200,
          "data": {
              "list（数据列表）": [
                  {
                      "id(主键id)": 4,
                      "number（数量）": 5400,
                      "total（获得礼物价值）": 766898,
                      "cost（投入金币）": 1080000,
                      "pool_type（奖池类型）": 3,
                      "summary_date（日期）": "2020-07-16",
                      "user_code（用户编号）": 793781,
                      "head_picture（用户头像）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/b5311f1699be8ca04664f591ae4a2e1e.png",
                      "nickname（昵称）": "毛22",
                      "pot_name（奖池名称）": "2元奖池",
                      "ratio（产出比）": 0.71
                  }
              ],
              "meta": {
                  "page": 1,
                  "limit": 20,
                  "next": null,
                  "total": 1
              },
              "header（奖池类型列表）": {
                  "3": "2元奖池",
                  "4": "2元独立奖池"
              }
          }
      }
     *
     */

    public function userDailySummary(Request $request) {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'user_code' => 'filled|numeric',
                'nickname' => 'filled',
                'pool_type' => 'filled|in:3,4',
                'start_time' => 'filled',
                'end_time' => 'filled',
                'page' => 'filled|numeric',
                'perPage' => 'filled|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $user_code = $request->input('user_code', '');
        $nickname = $request->input('nickname', '');
        $pool_type = $request->input('pool_type', '');
        $start_time = $request->input('start_time', '');
        $end_time = $request->input('end_time', '');
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $this->Summary();
        $where_user = [];
        if ($user_code) {
            $where_user[] = ['user_code', $user_code];
        }
        if ($nickname) {
            $where_user[] = [function ($query) use ($nickname) {
                $query->where('nickname', 'like', "%{$nickname}%");
            }];
        }
        if ($pool_type) {
            $where_game[] = ['pool_type', $pool_type];
        }
        $time = CommonService::transTime($start_time, $end_time);
        $start_time = date('Y-m-d', $time['start_at']);
        $end_time = date('Y-m-d', $time['end_at']);
        $search_time = [$start_time, $end_time];
        $where_game[] = [function ($query) use ($search_time) {
            $query->whereBetween('summary_date', $search_time);
        }];

        $list = app('repo_game_log_day')->userDailySummaryList($where_game, $where_user, $page, $perPage);
        if ($list) {
            foreach ($list as $key => &$value) {
                $value->user_code = $value->user_data->user_code;
                $value->head_picture = $value->user_data->head_picture;
                $value->nickname = $value->user_data->nickname;
                $value->pot_name = $this->pool_array[$value->pool_type];
                $value->ratio = $value->cost > 0 ? round($value->total / $value->cost, 2) : 0;
                unset($value->user_data);
                unset($value->user_id);
            }
        }
        $pool_array = $this->pool_array;
        return Response::sendList($list, [], $pool_array);
    }


    /**
     * @api {post} /new_fish/total_daily_summary  新钓鱼用户产出总汇总
     * @apiVersion 0.1.0
     * @apiName 新钓鱼用户产出总汇总
     * @apiGroup 游戏管理
     * @apiParam {number} user_code  用户编号，非必传
     * @apiParam {string} nickname  用户昵称，非必传
     * @apiParam {number} page  页数，非必传
     * @apiParam {number} perPage  每页数量，非必传
     * @apiSuccessExample 成功响应
        {
            "code": 200,
            "data": {
                "list(数据列表)": [
                    {
                        "pool_type（奖池类型）": 3,
                        "summary_date（时间）": "2020-07-16",
                        "total（获得总金币）": "1287008",
                        "cost（花费总金币）": "1080600",
                        "number（奖品数量）": "5403",
                        "user_code（用户编号）": 793781,
                        "head_picture（用户头像）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/b5311f1699be8ca04664f591ae4a2e1e.png",
                        "nickname（昵称）": "毛22",
                        "ratio（产出比）": 1.19
                    }
                ],
                "meta": {
                    "page": 1,
                    "limit": 20,
                    "next": null,
                    "total": 3
                }
            }
        }
     *
     */
    public function totalDailySummary(Request $request) {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'user_code' => 'filled|numeric',
                'nickname' => 'filled',
                'page' => 'filled|numeric',
                'perPage' => 'filled|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $user_code = $request->input('user_code', '');
        $nickname = $request->input('nickname', '');
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $this->Summary();
        $where_user = [];
        if ($user_code) {
            $where_user[] = ['user_code', $user_code];
        }
        if ($nickname) {
            $where_user[] = [function ($query) use ($nickname) {
                $query->where('nickname', 'like', "%{$nickname}%");
            }];
        }
        $list = app('repo_game_log_day')->totalDailySummaryList($where_user, $page, $perPage);
        if ($list) {
            foreach ($list as $key => &$value) {
                $value->user_code = $value->user_data->user_code;
                $value->head_picture = $value->user_data->head_picture;
                $value->nickname = $value->user_data->nickname;
                $value->ratio = $value->cost > 0 ? round($value->total / $value->cost, 2) : 0;
                unset($value->user_data);
                unset($value->user_id);
            }
        }
        return Response::sendList($list);
    }

    /**
     * @api {post} /new_fish/pool_total_summary  新钓鱼奖池产出比总汇总
     * @apiVersion 0.1.0
     * @apiName 新钓鱼奖池产出比总汇总
     * @apiGroup 游戏管理
     * @apiParam {number} pool_type  奖池类型，非必传
     * @apiParam {string} start_time  开始时间，非必传
     * @apiParam {string} end_time  开始时间，非必传
     * @apiParam {number} page  页数，非必传
     * @apiParam {number} perPage  每页数量，非必传
     * @apiSuccessExample 成功响应
        {
            "code": 200,
            "data": {
                "list(数据列表)": [
                    {
                        "pool_type（奖池类型）": 3,
                        "user_id（用户id）": 642741,
                        "summary_date（时间）": "2020-07-17",
                        "total（获得总金币）": "550746",
                        "cost（投入总金币）": "802800",
                        "number（数量）": "4014",
                        "pot_name（奖池名字）": "2元奖池",
                        "ratio（产出比）": 0.69,
                        "profit（平台收益）": -252054
                    }
                ],
                "meta": {
                    "page": 1,
                    "limit": 20,
                    "next": null,
                    "total": 3
                },
                "header（奖池类型列表）": {
                    "3": "2元奖池",
                    "4": "2元独立奖池"
                }
            }
        }

     */

    public  function  poolTotalSummary(Request $request){
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'pool_type' => 'filled|in:3,4',
                'start_time' => 'filled',
                'end_time' => 'filled',
                'page' => 'filled|numeric',
                'perPage' => 'filled|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $pool_type = $request->input('pool_type', '');
        $start_time = $request->input('start_time', '');
        $end_time = $request->input('end_time', '');
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $this->Summary();
        if ($pool_type) {
            $where_game[] = ['pool_type', $pool_type];
        }
        $time = CommonService::transTime($start_time, $end_time);
        $start_time = date('Y-m-d', $time['start_at']);
        $end_time = date('Y-m-d', $time['end_at']);
        $search_time = [$start_time, $end_time];
        $where_game[] = [function ($query) use ($search_time) {
            $query->whereBetween('summary_date', $search_time);
        }];

        $list = app('repo_game_log_day')->poolTotalSummary($where_game, $page, $perPage);
        if ($list) {
            foreach ($list as $key => &$value) {
                $value->pot_name = $this->pool_array[$value->pool_type];
                $value->ratio = $value->cost > 0 ? round($value->total / $value->cost, 2) : 0;
                $value->profit =$value->total -$value->cost  ;
            }
        }
        $pool_array = $this->pool_array;
        return Response::sendList($list,[],$pool_array);
    }


    public function Summary() {
        app('repo_game_log_day')->clearTable();
        $userDailySummary = app('repo_game_log')->userDailySummary();
        if ($userDailySummary) {
            app('repo_game_log_day')->addAll($userDailySummary);
        }
    }


}

